package net.bxx2004.pandalib.bukkit.gui.predefine;

import net.bxx2004.pandalib.PandaLib;
import net.bxx2004.pandalib.bukkit.file.PYml;
import net.bxx2004.pandalib.bukkit.item.PItemStack;
import net.bxx2004.pandalib.bukkit.language.abandon.PTitle;
import net.bxx2004.pandalib.bukkit.language.application.PConversation;
import net.bxx2004.pandalib.bukkit.listener.PListener;
import net.bxx2004.pandalib.bukkit.util.PMath;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;

public class YmlEditMenu {
    private PYml data;
    private YmlMenu menu;
    public String cache = "";
    public PConversation conversation;
    public YmlEditMenu(PYml yml){
        data = yml;
        menu = new YmlMenu(yml,this);
        conversation = new PConversation("请输入要修改的内容",false);
        update(null);
        new PListener(){
            @EventHandler
            public void close(InventoryCloseEvent event){
                if (event.getInventory().getHolder().equals(menu.holder())){
                    data.reloadYaml();
                }
            }
        }.hook(PandaLib.initPlugin.getName());
    }
    public void update(String node){
        for (int i = 0; i < PMath.formatNumber(key(node).size(),35); i++){
            PItemStack[] stacks = new PItemStack[35];
            for (int a = 0; a < 35;a++){
                try {
                    String n = key(node).get(a + (i*key(node).size()));
                    if (key(cache + n).size() == 0){
                        stacks[a] = new PItemStack(Material.PAPER,"&7"+n,"&9数据", " ","&c⇨ 值: " + data.get(cache + n).toString(), "&c⇨ 点击编辑该数据");
                    }else {
                        stacks[a] = new PItemStack(Material.STICK,"&7"+n,"&9节点", " ","&c⇨ 子节点: " + key(cache + n).toString(), "&c⇨ 点击浏览该节点内容");
                    }
                }catch (Exception e){}
            }
            menu.setItem(i,stacks);
        }
        menu.layout();
    }
    public PYml getData() {
        return data;
    }
    public void open(Player player){
        menu.open(player);
    }
    private List<String> key(String parent){
        List<String> list = new ArrayList<>();
        try {
            if (parent == null){
                for (String a :data.getYaml().getKeys(false)){
                    list.add(a);
                }
                return list;
            }else {
                for (String a :data.getYaml().getConfigurationSection(parent).getKeys(false)){
                    list.add(a);
                }
                return list;
            }
        }catch (Exception e){
            return new ArrayList<>();
        }
    }
}
class YmlMenu extends FlipMenu{
    private YmlEditMenu edition;
    public YmlMenu(PYml yml,YmlEditMenu ymlEdition){
        super("正在编辑: " + yml.getName());
        this.edition = ymlEdition;
    }

    @Override
    public void click(InventoryClickEvent event) {
        super.click(event);
        try {
            if (event.getCurrentItem() != null){
                event.getWhoClicked().closeInventory();
                String node = event.getCurrentItem().getItemMeta().getDisplayName().substring(2);
                if (event.getCurrentItem().getItemMeta().getLore().get(0).contains("数据")){
                    edition.conversation.start((Player) event.getWhoClicked());
                    new BukkitRunnable(){
                        @Override
                        public void run() {
                            if (edition.conversation.answer() == null){

                            }else {
                                String a = edition.conversation.answer();
                                edition.getData().set(edition.cache + node,a);
                                edition.cache = "";
                                edition.update(null);
                                open((Player) event.getWhoClicked());
                                PTitle.To((Player) event.getWhoClicked(),"修改成功");
                                cancel();
                            }
                        }
                    }.runTaskTimer(PandaLib.initPlugin,0,20);
                }else {
                    edition.cache = edition.cache + node +".";
                    edition.update(edition.cache.substring(0,edition.cache.length()-1));
                    event.getWhoClicked().closeInventory();
                    open((Player) event.getWhoClicked());
                }
            }
        }catch (Exception e){}
    }

    @Override
    public int[] notClickSlot() {
        return new int[]{0,9,18,27,36,45,46,47,48,49,50,51,52,53,8,17,26,35,44};
    }

    @Override
    public String[] notClickString() {
        return new String[]{"节点","数据"};
    }

}
